# setwd("/Users/johanneskarreth/Documents/Dropbox/Uni/1 - Papers/36 - Democratic reversals/Paper/Project/JPR submission/R&R/Data analysis")

m3dl <- readRDS("Work/m3dl_forJAGS.rds")

m3dl$b0 <- c(rep(0, 13))
m3dl$B0 <- diag(rep(1), 13)

m3.params <- c("b")

m3.inits1 <- list("b" = c(rep(0, 13)), "tt" = m3dl$max_terrthreatfm_mc1946pp_pre2_ln)
m3.inits2 <- list("b" = c(rep(0, 13)), "tt" = m3dl$max_terrthreatfm_mc1946pp_pre2_ln)
m3.inits3 <- list("b" = c(rep(0, 13)), "tt" = m3dl$max_terrthreatfm_mc1946pp_pre2_ln)
m3.inits4 <- list("b" = c(rep(0, 13)), "tt" = m3dl$max_terrthreatfm_mc1946pp_pre2_ln)
m3.inits <- list(m3.inits1, m3.inits2, m3.inits3, m3.inits4)

library("dclone")

cl <- makePSOCKcluster(4)
main3.1946.fitjags.par <- jags.parfit(cl = cl,
                                      data = m3dl,
                                      params = m3.params,
                                      model = "Scripts/m3_jags.jags",
                                      inits = m3.inits,
                                      n.chains = 4,
                                      n.iter = 10000, n.burnin = 2000, n.thin = 10)

saveRDS(main3.1946.fitjags.par, file = "Work/main3jags1946.rds")

# m3c.fitjags <- jags(data = m3dl, parameters.to.save = m3.params, n.chains = 4,
#                     inits = m3.inits, n.iter = 100000, n.burnin = 2000, n.thin = 10,
#                     model.file = "Scripts/m3_jags.jags")

library("BayesPostEst")

mcmcTab(main3.1946.fitjags.par, Pr = TRUE)
